package cn.ht.jywrwater.module.ht.controller.admin.waterqualitydevicefactor;

import cn.ht.jywrwater.framework.common.pojo.CommonResult;
import cn.ht.jywrwater.framework.common.pojo.PageResult;
import cn.ht.jywrwater.framework.excel.core.util.ExcelUtils;
import cn.ht.jywrwater.framework.operatelog.core.annotations.OperateLog;
import cn.ht.jywrwater.module.ht.controller.admin.waterqualitydevicefactor.vo.*;
import cn.ht.jywrwater.module.ht.convert.waterqualitydevicefactor.WaterQualityDeviceFactorConvert;
import cn.ht.jywrwater.module.ht.dal.dataobject.waterqualitydevicefactor.WaterQualityDeviceFactorDO;
import cn.ht.jywrwater.module.ht.service.waterqualitydevicefactor.WaterQualityDeviceFactorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.ht.jywrwater.framework.common.pojo.CommonResult.success;
import static cn.ht.jywrwater.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 水质设备因子")
@RestController
@RequestMapping("/ht/water-quality-device-factor")
@Validated
public class WaterQualityDeviceFactorController {

    @Resource
    private WaterQualityDeviceFactorService waterQualityDeviceFactorService;

    @PostMapping("/create")
    @Operation(summary = "创建水质设备因子")
    @PreAuthorize("@ss.hasPermission('ht:water-quality-device-factor:create')")
    public CommonResult<Long> createWaterQualityDeviceFactor(@Valid @RequestBody WaterQualityDeviceFactorCreateReqVO createReqVO) {
        return success(waterQualityDeviceFactorService.createWaterQualityDeviceFactor(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新水质设备因子")
    @PreAuthorize("@ss.hasPermission('ht:water-quality-device-factor:update')")
    public CommonResult<Boolean> updateWaterQualityDeviceFactor(@Valid @RequestBody WaterQualityDeviceFactorUpdateReqVO updateReqVO) {
        waterQualityDeviceFactorService.updateWaterQualityDeviceFactor(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除水质设备因子")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ht:water-quality-device-factor:delete')")
    public CommonResult<Boolean> deleteWaterQualityDeviceFactor(@RequestParam("id") Long id) {
        waterQualityDeviceFactorService.deleteWaterQualityDeviceFactor(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得水质设备因子")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ht:water-quality-device-factor:query')")
    public CommonResult<WaterQualityDeviceFactorRespVO> getWaterQualityDeviceFactor(@RequestParam("id") Long id) {
        WaterQualityDeviceFactorDO waterQualityDeviceFactor = waterQualityDeviceFactorService.getWaterQualityDeviceFactor(id);
        return success(WaterQualityDeviceFactorConvert.INSTANCE.convert(waterQualityDeviceFactor));
    }

    @GetMapping("/list")
    @Operation(summary = "获得水质设备因子列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('ht:water-quality-device-factor:query')")
    public CommonResult<List<WaterQualityDeviceFactorRespVO>> getWaterQualityDeviceFactorList(@RequestParam("ids") Collection<Long> ids) {
        List<WaterQualityDeviceFactorDO> list = waterQualityDeviceFactorService.getWaterQualityDeviceFactorList(ids);
        return success(WaterQualityDeviceFactorConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得水质设备因子分页")
    @PreAuthorize("@ss.hasPermission('ht:water-quality-device-factor:query')")
    public CommonResult<PageResult<WaterQualityDeviceFactorRespVO>> getWaterQualityDeviceFactorPage(@Valid WaterQualityDeviceFactorPageReqVO pageVO) {
        PageResult<WaterQualityDeviceFactorDO> pageResult = waterQualityDeviceFactorService.getWaterQualityDeviceFactorPage(pageVO);
        return success(WaterQualityDeviceFactorConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出水质设备因子 Excel")
    @PreAuthorize("@ss.hasPermission('ht:water-quality-device-factor:export')")
    @OperateLog(type = EXPORT)
    public void exportWaterQualityDeviceFactorExcel(@Valid WaterQualityDeviceFactorExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<WaterQualityDeviceFactorDO> list = waterQualityDeviceFactorService.getWaterQualityDeviceFactorList(exportReqVO);
        // 导出 Excel
        List<WaterQualityDeviceFactorExcelVO> datas = WaterQualityDeviceFactorConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "水质设备因子.xls", "数据", WaterQualityDeviceFactorExcelVO.class, datas);
    }

}
